package com.platform.qujia.controller;

import com.platform.qujia.enums.CardRecordType;
import com.platform.qujia.enums.ElectronCardType;
import com.platform.qujia.enums.PayTypeEnum;
import com.platform.qujia.pojo.CustomerCardUseRecord;
import com.platform.qujia.pojo.PayOrder;
import com.platform.qujia.pojo.vo.RefundResultVO;
import com.platform.qujia.service.PosRefundService;
import com.platform.qujia.utils.MathUtil;
import com.platform.qujia.utils.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @date 2023/11/13
 */
@RestController
@RequestMapping("pos")
public class PosRefundController {

    @Autowired
    private PosRefundService posRefundService;

    /**
     * 查询24小时内最后一笔支付消费的订单
     * @return
     */
    @GetMapping("getLastPaidConsumeOrder")
    public ResultData<PayOrder> getLastPaidConsumeOrder(@RequestParam String mobilePhone,
                                                        @RequestParam Integer stationId,
                                                        @RequestParam(required = false) Integer consumerId){
        PayOrder order = posRefundService.getLastPaidConsumeOrder(mobilePhone, stationId, consumerId);
        if (order != null) {
            order.setPayType(PayTypeEnum.getByType(order.getPayType(),PayTypeEnum.def).getDes());
//            BigDecimal disAmt = order.getDiscountAmt() == null ? BigDecimal.ZERO : BigDecimal.valueOf(order.getDiscountAmt());
//            BigDecimal coupAmt = order.getDiscountCouponAmt() == null ? BigDecimal.ZERO : BigDecimal.valueOf(order.getDiscountCouponAmt());
//            order.setDiscountAmt(MathUtil.add(disAmt,coupAmt).setScale(2, RoundingMode.HALF_UP).doubleValue());
        }
        return ResultData.success(order);
    }

    /**
     * 根据订单号查询
     * @param orderNo
     * @param stationId
     * @return
     */
    @GetMapping("getOneOrder")
    public ResultData<PayOrder> getOneOrder(@RequestParam String orderNo,
                                            @RequestParam Integer stationId){
        PayOrder order = posRefundService.getOneOrder(orderNo,stationId);
        if (order != null) {
            order.setPayType(PayTypeEnum.getByType(order.getPayType(),PayTypeEnum.def).getDes());
//            BigDecimal disAmt = order.getDiscountAmt() == null ? BigDecimal.ZERO : BigDecimal.valueOf(order.getDiscountAmt());
//            BigDecimal coupAmt = order.getDiscountCouponAmt() == null ? BigDecimal.ZERO : BigDecimal.valueOf(order.getDiscountCouponAmt());
//            order.setDiscountAmt(MathUtil.add(disAmt,coupAmt).setScale(2, RoundingMode.HALF_UP).doubleValue());
        }
        return ResultData.success(order);
    }

    /**
     * 退款消费订单
     * @return
     */
    @GetMapping("refundConsumeOrder")
    public ResultData<RefundResultVO> refundConsumeOrder(@RequestParam Integer orderId,
                                                         @RequestParam Integer userId){
        return ResultData.success(posRefundService.refundConsumeOrder(orderId,userId));
    }

    /**
     * 查询24小时内最后一笔支付消费的订单
     * @return
     */
    @GetMapping("getLastPaidRechargeOrder")
    public ResultData<CustomerCardUseRecord> getLastPaidRechargeOrder(@RequestParam String mobilePhone,
                                                                      @RequestParam Integer stationId,
                                                                      @RequestParam(required = false) Integer consumerId){
        CustomerCardUseRecord rechargeOrder = posRefundService.getLastPaidRechargeOrder(mobilePhone, stationId, consumerId);
        if (rechargeOrder != null) {
            rechargeOrder.setPayType(CardRecordType.getByKey(rechargeOrder.getPayType(),CardRecordType.WX_RECHARGE).getName());
            rechargeOrder.setCardOilsType(ElectronCardType.getByKey(rechargeOrder.getCardOilsType(),ElectronCardType.GAS).getName()+"卡");
        }
        return ResultData.success(rechargeOrder);
    }

    /**
     * 根据订单号查询充值订单
     * @param orderNo
     * @param stationId
     * @return
     */
    @GetMapping("getOneRechargeOrder")
    public ResultData<CustomerCardUseRecord> getOneRechargeOrder(@RequestParam String orderNo,
                                                                 @RequestParam Integer stationId){
        CustomerCardUseRecord rechargeOrder = posRefundService.getOneRechargeOrder(orderNo,stationId);
        if (rechargeOrder != null) {
            rechargeOrder.setPayType(CardRecordType.getByKey(rechargeOrder.getPayType(),CardRecordType.WX_RECHARGE).getName());
            rechargeOrder.setCardOilsType(ElectronCardType.getByKey(rechargeOrder.getCardOilsType(),ElectronCardType.GAS).getName()+"卡");
        }
        return ResultData.success(rechargeOrder);
    }


    /**
     * 退款充值订单
     * @return
     */
    @GetMapping("refundRechargeOrder")
    public ResultData<RefundResultVO> refundRechargeOrder(@RequestParam Integer orderId,
                                                          @RequestParam Integer userId){
        return ResultData.success(posRefundService.refundRechargeOrder(orderId, userId));
    }

}
